#include "StdAfx.h"
#include "CImgLibrary.h"


CImgLibrary::CImgLibrary(int width,int height)
{
	canvas = CImg<int>(width,height,1,255);
	canvas.fill(0);
}


CImgLibrary::~CImgLibrary(void)
{
	if (canvas != NULL)
		delete canvas;
}

void CImgLibrary::DrawLine(int x1, int y1, int x2, int y2, COLORREF color, int strokewidth)
{
	unsigned char colorArr[] = {(unsigned char)GetRValue(color),(unsigned char)GetGValue(color),(unsigned char)GetBValue(color)};
	for(unsigned int i=0 ; i<strokewidth; i++)
		canvas.draw_line(x1-i, y1+i, x2-i, y2+i, colorArr);
	CImgDisplay display(canvas, "CImg", 0);
	while (!display.is_closed())
		display.wait();
}
void CImgLibrary::DrawRectangle(int x, int y, int width, int height, COLORREF color, int strokewidth, COLORREF fill)
{
	unsigned char colorArr[] = {(unsigned char)GetRValue(color),(unsigned char)GetGValue(color),(unsigned char)GetBValue(color)};
	if(strokewidth == 1)
		strokewidth++;
	canvas.draw_rectangle(x, y - strokewidth/2, x + width, y, colorArr);
	canvas.draw_rectangle(x, y + height, x + width, y + height + strokewidth/2, colorArr);
	canvas.draw_rectangle(x - strokewidth/2, y - strokewidth/2, x, y + height + strokewidth/2, colorArr);
	canvas.draw_rectangle(x + width, y - strokewidth/2, x + width + strokewidth/2, y + height + strokewidth/2, colorArr);
	unsigned char fillArr[] = {(unsigned char)GetRValue(fill),(unsigned char)GetGValue(fill),(unsigned char)GetBValue(fill)};
	canvas.draw_rectangle(x, y, x + width, y + height, fillArr);
	CImgDisplay display(canvas, "CImg", 0);
	while (!display.is_closed())
		display.wait();
}
void CImgLibrary::DrawEllipse(int cx, int cy, int rx, int ry, COLORREF color, int strokewidth, COLORREF fill)
{
	unsigned char colorArr[] = {(unsigned char)GetRValue(color),(unsigned char)GetGValue(color),(unsigned char)GetBValue(color)};
	unsigned char fillArr[] = {(unsigned char)GetRValue(fill),(unsigned char)GetGValue(fill),(unsigned char)GetBValue(fill)};
	for(int i=0; i<strokewidth; i++)
		canvas.draw_ellipse(cx,cy,rx+i/2,ry+i/2,0,colorArr,100,1);
	canvas.draw_ellipse(cx,cy,rx,ry,0,colorArr,100,1).draw_fill(cx, cy, fillArr);
	CImgDisplay display(canvas, "CImg", 0);
	while (!display.is_closed())
		display.wait();
}

IGraphicLibrary* CImgLibrary::Clone( )
{
	return new CImgLibrary(this->canvas.width(),
		this->canvas.height());
}
